package com.jk.myimkit.imkit;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;

import com.jk.myimkit.R;
import com.jk.myimkit.imageloader.core.DisplayImageOptions;
import com.jk.myimkit.imkit.manager.AudioPlayManager;
import com.jk.myimkit.imkit.model.UIMessage;
import com.jk.myimkit.imkit.userInfoCache.RongUserInfoManager;
import com.jk.myimkit.imkit.widget.provide.MessageItemLongClickAction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import io.rong.common.RLog;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.location.message.RealTimeLocationStartMessage;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UnknownMessage;
import io.rong.imlib.model.UserInfo;
import io.rong.message.HandshakeMessage;
import io.rong.message.NotificationMessage;
import io.rong.message.PublicServiceMultiRichContentMessage;
import io.rong.message.PublicServiceRichContentMessage;
import io.rong.message.RecallNotificationMessage;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;

public class RongMessageItemLongClickActionManager {
    private static final String TAG = RongMessageItemLongClickActionManager.class.getSimpleName();
    private List<MessageItemLongClickAction> messageItemLongClickActions;

    public static RongMessageItemLongClickActionManager getInstance() {
        return RongMessageItemLongClickActionManager.Holder.instance;
    }

    private RongMessageItemLongClickActionManager() {
    }

    public void init() {
        if (this.messageItemLongClickActions == null) {
            this.messageItemLongClickActions = new ArrayList();
            this.initCommonMessageItemLongClickActions();
        }

    }

    public List<MessageItemLongClickAction> getMessageItemLongClickActions() {
        return this.messageItemLongClickActions;
    }

    public void addMessageItemLongClickAction(MessageItemLongClickAction action) {
        this.addMessageItemLongClickAction(action, -1);
    }

    public void addMessageItemLongClickAction(MessageItemLongClickAction action, int index) {
        if (this.messageItemLongClickActions.contains(action)) {
            this.messageItemLongClickActions.remove(action);
        }

        if (index < 0) {
            this.messageItemLongClickActions.add(action);
        } else {
            this.messageItemLongClickActions.add(index, action);
        }

    }

    public void removeMessageItemLongClickAction(MessageItemLongClickAction action) {
        this.messageItemLongClickActions.remove(action);
    }

    public List<MessageItemLongClickAction> getMessageItemLongClickActions(UIMessage uiMessage) {
        List<MessageItemLongClickAction> actions = new ArrayList();
        Iterator var3 = this.messageItemLongClickActions.iterator();

        while(var3.hasNext()) {
            MessageItemLongClickAction action = (MessageItemLongClickAction)var3.next();
            if (action.filter(uiMessage)) {
                actions.add(action);
            }
        }

        return actions;
    }

    private void initCommonMessageItemLongClickActions() {
        MessageItemLongClickAction messageItemLongClickAction = (new MessageItemLongClickAction.Builder()).titleResId(R.string.rc_dialog_item_message_copy).actionListener(new MessageItemLongClickAction.MessageItemLongClickListener() {
            public boolean onMessageItemLongClick(Context context, UIMessage message) {
                ClipboardManager clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
                if (message.getContent() instanceof RecallNotificationMessage) {
                    return false;
                } else {
                    clipboard.setText(((TextMessage)message.getContent()).getContent());
                    return true;
                }
            }
        }).showFilter(new MessageItemLongClickAction.Filter() {
            public boolean filter(UIMessage message) {
                return message.getContent() instanceof TextMessage;
            }
        }).build();
        this.addMessageItemLongClickAction(messageItemLongClickAction);
        messageItemLongClickAction = (new MessageItemLongClickAction.Builder()).titleResId(R.string.rc_dialog_item_message_recall).actionListener(new MessageItemLongClickAction.MessageItemLongClickListener() {
            public boolean onMessageItemLongClick(Context context, UIMessage message) {
                RongIM.getInstance().recallMessage(message.getMessage(), RongMessageItemLongClickActionManager.this.getPushContent(context, message));
                return true;
            }
        }).showFilter(new MessageItemLongClickAction.Filter() {
            public boolean filter(UIMessage message) {
                if (!(message.getContent() instanceof NotificationMessage) && !(message.getContent() instanceof HandshakeMessage) && !(message.getContent() instanceof PublicServiceRichContentMessage) && !(message.getContent() instanceof RealTimeLocationStartMessage) && !(message.getContent() instanceof UnknownMessage) && !(message.getContent() instanceof PublicServiceMultiRichContentMessage) && !message.getSentStatus().equals(Message.SentStatus.CANCELED)) {
                    long deltaTime = RongIM.getInstance().getDeltaTime();
                    long normalTime = System.currentTimeMillis() - deltaTime;
                    boolean enableMessageRecall = false;
                    int messageRecallInterval = -1;
                    boolean hasSent = !message.getSentStatus().equals(Message.SentStatus.SENDING) && !message.getSentStatus().equals(Message.SentStatus.FAILED);

                    try {
                        enableMessageRecall = RongContext.getInstance().getResources().getBoolean(R.bool.rc_enable_message_recall);
                        messageRecallInterval = RongContext.getInstance().getResources().getInteger(R.integer.rc_message_recall_interval);
                    } catch (Resources.NotFoundException var10) {
                        RLog.e(RongMessageItemLongClickActionManager.TAG, "rc_message_recall_interval not configure in rc_config.xml");
                        var10.printStackTrace();
                    }

                    return hasSent && enableMessageRecall && normalTime - message.getSentTime() <= (long)(messageRecallInterval * 1000) && message.getSenderUserId().equals(RongIM.getInstance().getCurrentUserId()) && !message.getConversationType().equals(Conversation.ConversationType.CUSTOMER_SERVICE) && !message.getConversationType().equals(Conversation.ConversationType.APP_PUBLIC_SERVICE) && !message.getConversationType().equals(Conversation.ConversationType.PUBLIC_SERVICE) && !message.getConversationType().equals(Conversation.ConversationType.SYSTEM) && !message.getConversationType().equals(Conversation.ConversationType.CHATROOM);
                } else {
                    return false;
                }
            }
        }).build();
        this.addMessageItemLongClickAction(messageItemLongClickAction);
        messageItemLongClickAction = (new MessageItemLongClickAction.Builder()).titleResId(R.string.rc_dialog_item_message_delete).actionListener(new MessageItemLongClickAction.MessageItemLongClickListener() {
            public boolean onMessageItemLongClick(Context context, UIMessage message) {
                if (message.getMessage().getContent() instanceof VoiceMessage) {
                    Uri uri = ((VoiceMessage)message.getMessage().getContent()).getUri();
                    Uri playingUri = AudioPlayManager.getInstance().getPlayingUri();
                    if (playingUri != null && playingUri == uri) {
                        AudioPlayManager.getInstance().stopPlay();
                    }
                }

                RongIM.getInstance().deleteMessages(new int[]{message.getMessageId()}, (RongIMClient.ResultCallback)null);
                return true;
            }
        }).build();
        this.addMessageItemLongClickAction(messageItemLongClickAction);
    }

    private String getPushContent(Context context, UIMessage message) {
        String userName = "";
        UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(message.getSenderUserId());
        if (userInfo != null) {
            userName = userInfo.getName();
        }

        return context.getString(R.string.rc_user_recalled_message, new Object[]{userName});
    }

    private static class Holder {
        static RongMessageItemLongClickActionManager instance = new RongMessageItemLongClickActionManager();

        private Holder() {
        }
    }
}
